package com.pingpang.registry;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.apache.zookeeper.KeeperException;

public class SeverRegistry {

	/**
	 * 1.server的根目录
	 * 2.子目录以接口的名字命名->值位IP:PORT
	 */
	private static final String PINGPANG_REGIST_SERVER="/PINGPANG_REGIST_SERVER";
	
	/** zookeeper地址 */
	private static final String CONNECT_ADDR = "127.0.0.1:2181";
	 /** session超时时间 */
    private static final int SESSION_OUTTIME = 10000;//ms
	
	private ZkClient zc;
	
	private static SeverRegistry serverRegistry;

    private SeverRegistry() {};
    
    public static SeverRegistry getServerRegistry() {
    	if(null==serverRegistry) {
    		serverRegistry=new SeverRegistry();
    	}
    	
    	if(null==serverRegistry.zc) {
				serverRegistry.zc = new ZkClient(new ZkConnection(SeverRegistry.CONNECT_ADDR), SeverRegistry.SESSION_OUTTIME);
    	}
    	return serverRegistry;
    }
    
	/**
	  *   添加节点
	 * @param serviceSet 服务名称
	 * @param value 路径
	 * @return
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	public boolean registAddServer(Set<String>serviceSet,String value){
        if(null==serviceSet || serviceSet.isEmpty() || null==value || "".equals(value.trim())) {
        	return false;
        }
        
        for(String str : serviceSet) {
        	this.crateNode(str, value);
        }
        
        return true;
	}
	
	/**
	 *    移除节点
	 * @param serviceSet 服务名称
	 * @param value 路径
	 * @return
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	public boolean removeAddServer(Set<String>serviceSet,String value) throws KeeperException, InterruptedException {
		 if(null==serviceSet || serviceSet.isEmpty() || null==value || "".equals(value.trim())) {
	        	return false;
	        }
	        
	        for(String str : serviceSet) {
	        	this.removeNode(str, value);
	        }
	        
	        return true;
	}
	
	/**
	   *  创建节点
	 *    路径为  PINGPANG_REGIST_SERVER
	 *                            com.test
	 *                                    127.0.0.1
	 *                                    127.0.0.2  
	 * @param path
	 * @param value
	 * @return
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	private boolean crateNode(String path,String value){
		if(null==path || "".equals(path) || null==value || "".equals(value.trim())) {
			return false;
		}
		
		String createPath=SeverRegistry.PINGPANG_REGIST_SERVER+"/"+path;
		
		if(!this.isExit(SeverRegistry.PINGPANG_REGIST_SERVER+"/"+path)) {
			this.zc.createPersistent(createPath, true);
		}
		
		createPath+="/"+value;
		
		if(!this.isExit(createPath)) {
			this.zc.createEphemeral(createPath);
		 }
		
		return true;
	}
	
	public boolean removeNode(String path,String value){
		if(null==path || "".equals(path) || null==value || "".equals(value.trim())) {
			return false;
		}
		
		String createPath=SeverRegistry.PINGPANG_REGIST_SERVER+"/"+path+"/"+value;
		
		if(this.isExit(createPath)) {
			return zc.delete(createPath);
		}
		
		return false;
	}
	
	/**
	  * 判断节点是否存在
	 * @param node
	 * @return
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	private boolean isExit(String node){
		return this.zc.exists(node);
	}
	
	/**
	  * 获取接口下所有的服务器IP
	 * @param server
	 * @return
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	public List<String> getServerPath(String server){
		List<String> serverList=new LinkedList<String>();
		if(null==server || "".equals(server.trim())) {
			return serverList ;
		}
		String path=SeverRegistry.PINGPANG_REGIST_SERVER+"/"+server;
		return zc.getChildren(path);
	}
	
	/**
	 * 关闭连接
	 * @throws InterruptedException 
	 */
	public void close(){
       if(null!=zc) {
    	   zc.close();
       }   		
	}
	
    public static void main(String[] args){
    	SeverRegistry sr=SeverRegistry.getServerRegistry();
    	sr.crateNode("com.pingpang.test", "127.0.0.1:112");
    	sr.crateNode("com.pingpang.test", "127.0.0.1:113");
    	sr.crateNode("com.pingpang.test", "127.0.0.1:114");
    	sr.crateNode("com.pingpang.test", "127.0.0.1:115");
    	
		/*
		 * sr.removeNode("com.pingpang.test", "127.0.0.1:112");
		 * sr.removeNode("com.pingpang.test", "127.0.0.1:113");
		 * sr.removeNode("com.pingpang.test", "127.0.0.1:114");
		 * sr.removeNode("com.pingpang.test", "127.0.0.1:115");
		 * 
		 * sr.removeNode("com.pingpang.test", "");
		 */
		/*
		 * while(true) {
		 * 
		 * }
		 */
		
		List<String> server = sr.getServerPath("com.pingpang.test");
		for (String str : server) {
			System.out.println("===" + str);
		}
		
		for(;;) {}
	}	
	
}
